package com.lg.zookeeper.watch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

import java.util.concurrent.CountDownLatch;

/**
 * @author by Mr. Li 2020/12/10 14:08
 */
public class DefaultWatch implements Watcher {

    private CountDownLatch countDownLatch;

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void process(WatchedEvent event) {
        switch (event.getState()) {
            case Unknown:
            case AuthFailed:
            case ConnectedReadOnly:
            case SaslAuthenticated:
            case Expired:
            case Closed:
            case NoSyncConnected:
                break;
            case SyncConnected:
                countDownLatch.countDown();// 当客户端服务端同步成功，此处释放锁
                break;
        }
    }
}
